package com.mmw.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mmw.crm.bo.BusinessPageBo;
import com.mmw.crm.entity.Business;
import com.mmw.crm.result.Result;
import com.mmw.crm.service.BusinessService;
import com.mmw.crm.vo.CustomerVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class BusinessController {

    @Resource
    BusinessService businessService;

    @PostMapping("CrmBusiness/queryPageList")
    public Result crmBusinessQueryPageList(@RequestBody CustomerVo customerVo){

        Integer page = customerVo.getPage();
        Integer limit = customerVo.getLimit();
        String search = customerVo.getSearch();

        LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(search)){
            wrapper.like(Business::getBusinessName,search);
        }
        Page<Business> pageInfo = new Page<>(page,limit);
        businessService.page(pageInfo,wrapper);
        List<Business> records = pageInfo.getRecords();

        BusinessPageBo businessPageBo = new BusinessPageBo();
        businessPageBo.setTotalRow(records.size());
        businessPageBo.setList(records);

        return Result.build(records,null,0);

    }

    @PostMapping("CrmBusiness/deleteByIds")
    public Result crmBusinessDeleteByIds(@RequestParam List<Integer> businessIds){
        businessService.removeByIds(businessIds);
        return Result.build(null,null,0);
    }

}
